namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AdvicePage : Page
    {
        protected DataGrid AdviceListDg;
        protected DropDownList AreaDdl;
        protected Image iAdvice;
        protected Label lbArea;
        protected Button newBtn;
        protected UserPrincipal Principal = null;

        private void AdviceListDg_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string cmd = e.CommandName;
            string strAdviceId = e.CommandArgument.ToString();
            Guid adviceId = new Guid();
            try
            {
                adviceId = new Guid(strAdviceId);
            }
            catch (Exception)
            {
                return;
            }
            switch (cmd)
            {
                case "Edit":
                    base.Response.Redirect("AdviceEditPage.aspx?IsModify=True&AdviceId=" + adviceId.ToString(), true);
                    break;

                case "delete":
                    ManageAdvice.RemoveAdviceInfoViaId(adviceId);
                    if ((this.AdviceListDg.Items.Count == 1) && (this.AdviceListDg.CurrentPageIndex > 0))
                    {
                        this.AdviceListDg.CurrentPageIndex--;
                    }
                    this.AdviceListDgDataBind();
                    break;
            }
        }

        private void AdviceListDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.AdviceListDg.CurrentPageIndex = e.NewPageIndex;
            this.AdviceListDgDataBind();
        }

        private void AdviceListDgDataBind()
        {
            if (this.Principal.SysUser.IsAdmin > 0)
            {
                this.AdviceListDg.DataSource = ManageAdvice.GetAdviceInfoViaAreaId(new Guid(this.AreaDdl.SelectedValue));
            }
            else
            {
                this.AdviceListDg.DataSource = ManageAdvice.GetAdviceInfoViaUserId(this.Principal.SysUser.UserId);
            }
            this.AdviceListDg.DataBind();
        }

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.AdviceListDgDataBind();
        }

        private void FillPage()
        {
            this.AdviceListDgDataBind();
        }

        private void InitializeComponent()
        {
            this.newBtn.Click += new EventHandler(this.newBtn_Click);
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.AdviceListDg.ItemCommand += new DataGridCommandEventHandler(this.AdviceListDg_ItemCommand);
            this.AdviceListDg.PageIndexChanged += new DataGridPageChangedEventHandler(this.AdviceListDg_PageIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            this.AreaDdl.Items.Clear();
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
            this.SetFromImage();
            this.SetFrom();
            this.AdviceListDg.PageSize = 14;
        }

        private void newBtn_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("AdviceEditPage.aspx?IsNew=True", true);
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SelectAuth()
        {
            this.InitPage();
            this.FillPage();
            if (this.Principal.SysUser.IsAdmin > 0)
            {
                this.newBtn.Visible = false;
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.newBtn.Text = ManageSystemConfig.GetSystemSetLanguage("AdvicePage", "newBtn", language, path);
            this.lbArea.Text = ManageSystemConfig.GetSystemSetLanguage("AdvicePage", "lbArea", language, path);
            this.AdviceListDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("AdvicePage", "Time", language, path);
            this.AdviceListDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("AdvicePage", "AdviceTitle", language, path);
            this.AdviceListDg.Columns[2].HeaderText = ManageSystemConfig.GetSystemSetLanguage("AdvicePage", "LoginName", language, path);
            this.AdviceListDg.Columns[3].HeaderText = ManageSystemConfig.GetSystemSetLanguage("AdvicePage", "edit", language, path);
            this.AdviceListDg.Columns[4].HeaderText = ManageSystemConfig.GetSystemSetLanguage("AdvicePage", "delete", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iAdvice.ImageUrl = "images/title_advice.jpg";
                    break;

                case "en":
                    this.iAdvice.ImageUrl = "images/title_adviceEn.jpg";
                    break;
            }
        }
    }
}

